﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Text;
using System.Web.UI;
using System.Web.UI.HtmlControls;

namespace PmfWeb.Core.WebControls
{
    ///<summary>
    /// 输入控件
    ///</summary>
    [ToolboxData("<{0}:Input runat=server></{0}:Input>")]
    //[LicenseProvider(typeof(ExpireLicenseProvider))]
    public class Input : System.Web.UI.WebControls.TextBox, IAttributeAccessor
    {
        private AttributeCollection _coll;
        //private License _license;
        /// <summary>
        /// 
        /// </summary>
        public Input()
        {
            //_license = LicenseManager.Validate(typeof (Input), this);
        }
        public override void Dispose()
        {
            base.Dispose();
            //if(null != _license) _license.Dispose();
        }


        /// <summary>
        /// 属性集合
        /// </summary>
        public AttributeCollection Coll
        {
            get
            {
                if(null == _coll) _coll = new AttributeCollection();
                return _coll;
            }
        }

        #region IAttributeAccessor 接口要实现的成员
        ///<summary>
        /// 获取属性值
        ///</summary>
        ///<param name="key">属性名称</param>
        ///<returns>string类型的属性值</returns>
        public string GetAttribute(string key)
        {
            return Coll[key];
        }


        ///<summary>
        /// 给控件添加属性
        ///</summary>
        ///<param name="key">属性名</param>
        ///<param name="value">属性值</param>
        public void SetAttribute(string key, string value)
        {
            Coll[key] = value;
        }
        #endregion

        /// <summary>
        /// 
        /// </summary>
        /// <param name="writer"></param>
        protected override void Render(HtmlTextWriter writer)
        {
            if (String.IsNullOrEmpty(Coll.ToString()))
            {
                writer.Write(String.Format("<input type=\"text\" id=\"{0}\" value=\"请输入内容\"/>", this.UniqueID));
            }
            else
            {
                writer.Write(String.Format("<input type=\"text\" id=\"{1}\" {0} value=\"请输入内容\" />", _coll, this.UniqueID));
            }
            base.Render(writer);
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="e"></param>
        protected override void OnPreRender(EventArgs e)
        {
            if (null != Page)
            {
                if (!Page.ClientScript.IsStartupScriptRegistered("Common_resource_core_js"))
                    Page.ClientScript.RegisterClientScriptResource(typeof(Input), "Common.resource.core.js");
                RegisterClientCssResource("Common.resource.core.css");
            }
            base.OnPreRender(e);
        }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="path"></param>
        protected void RegisterClientCssResource(string path)
        {
            if (null != this.Page.Header)
            {
                foreach (Control c in this.Page.Header.Controls)
                {
                    if (c.ID == "core_css")
                    {
                        return;
                    }
                }
                HtmlGenericControl control = new HtmlGenericControl("link");
                control.Attributes["href"] = Page.ClientScript.GetWebResourceUrl(typeof(Input), path);
                control.Attributes["id"] = "core_css";
                control.Attributes["type"] = "text/css";
                control.Attributes["rel"] = "stylesheet";
                Page.Header.Controls.Add(control);
            }
        }
    }
}
